<template>
  <div class="popup-box" @click.self="showPopup = false" v-if="showPopup">
    <div class="popup-body">
      <div class="left">
        <div class="left-item_1 m-b-10">
          <div
            class="flex items-center justify-between font-weight-700 p-x-20 m-b-10"
          >
            <text>商品合计</text>
            <text>{{ hejiValue }}</text>
          </div>

          <scroll-view scroll-y style="max-height: 110rpx" class="m-b-10">
            <div class="left-scroll-box">
              <div v-for="(item, i) in 4" :key="i" class="item">
                <text>限时折扣</text>
                <text>
                  <text>12.00</text>
                  <text
                    class="iconfont icon-bianji-xianjin p-l-6 p-y-3 color-primary"
                  ></text>
                </text>
              </div>
            </div>
          </scroll-view>

          <div
            class="flex items-center justify-between font-weight-700 p-x-20 m-b-10"
          >
            <text>应收</text>
            <text>{{ yingshouValue }}</text>
          </div>
          <div
            class="flex items-center justify-between font-weight-700 p-x-20 m-b-10"
          >
            <text>已收</text>
            <text>{{ yishouValue }}</text>
          </div>

          <scroll-view scroll-y style="max-height: 110rpx" class="m-b-10">
            <div class="left-scroll-box">
              <div v-for="(item, i) in 4" :key="i" class="item">
                <text>限时折扣</text>
                <text>
                  <text>12.00</text>
                  <text class="iconfont icon-guanbi-yuan p-l-6 p-y-3"></text>
                </text>
              </div>
            </div>
          </scroll-view>
          <div
            class="flex items-center justify-between font-weight-700 p-x-20 m-b-10"
          >
            <text>未收</text>
            <text>160.00</text>
          </div>
        </div>

        <div
          class="flex items-center justify-between font-weight-700 p-x-20 m-b-10"
        >
          <text>扫码付</text>
          <text></text>
        </div>

        <div
          @click="switchSelectType(1)"
          :class="[
            'flex items-center justify-between  left-item_2 m-b-20',
            selectType == 1 && 'active',
          ]"
        >
          <text>实付</text>
          <div>
            <tk-input v-if="selectType == 1" class="tk-input" v-model="shifuValue" ref="tkInputRef" @blur="tkInputRef.focus()" />
            <text v-else>{{ shifuValue }}</text>
          </div>
          <!-- <div class="flex justify-end items-center">
            <text :class="[!shifuValue && 'color-3']">
              {{ shifuValue ? shifuValue : "0.00" }}
            </text>
            <div v-if="selectType == 1" class="cursor"></div>
          </div> -->
        </div>

        <div
          @click="switchSelectType(2)"
          :class="[
            'flex items-center justify-between  left-item_2',
            selectType == 2 && 'active',
          ]"
        >
          <text>支付码</text>

          <div>
            <tk-input v-if="selectType == 2" class="tk-input" v-model="zhifumaValue" ref="tkInputRef" @blur="tkInputRef.focus()" />
            <text v-else>{{ zhifumaValue }}</text>
          </div>
          <!-- <div class="flex justify-end items-center">
            <text :class="[!zhifumaValue && 'color-3']">
              {{ zhifumaValue ? zhifumaValue : "请扫描顾客付款码进行支付" }}
            </text>
            <div v-if="selectType == 2" class="cursor"></div>
          </div> -->
        </div>

        <div class="left-bottom">
          <div @click="ZhekouPopupRef.open()">整单折扣</div>
          <div @click="close">取消结算</div>
        </div>
      </div>
      <div class="right">
        <div class="member_box"></div>
        <div class="pay_list">
          <div class="pay_item color-1" v-for="(item, i) in 8" :key="i">
            扫码付
          </div>
          <div class="pay_item color-3" @click="openMorePayList">更多></div>
        </div>

        <div class="num-keyboard">
          <div class="flex flex-wrap gap-8 flex-1 w-0">
            <div @click="keyD(1)" class="num">1</div>
            <div @click="keyD(2)" class="num">2</div>
            <div @click="keyD(3)" class="num">3</div>

            <div @click="keyD(4)" class="num">4</div>
            <div @click="keyD(5)" class="num">5</div>
            <div @click="keyD(6)" class="num">6</div>

            <div @click="keyD(7)" class="num">7</div>
            <div @click="keyD(8)" class="num">8</div>
            <div @click="keyD(9)" class="num">9</div>

            <div @click="keyD('00')" class="num">00</div>
            <div @click="keyD('0')" class="num">0</div>
            <div @click="keyD('.')" class="num">.</div>
          </div>

          <div class="keyboard-right">
            <div @click="keyD('清除')" class="num">清除</div>
            <div @click="keyD('退格')" class="num">
              <span
                class="iconfont icon-shanchu_delete-two font-size-27 font-weight-700"
              ></span>
            </div>
            <div @click="onConfirm" class="num confirm">确定</div>
          </div>
        </div>
      </div>
    </div>
  </div>

  

  <PaytypeList ref="PaytypeListRef" />

  <ZhekouPopup ref="ZhekouPopupRef" />
  <ChuzhifukuanPopup  ref="ChuzhifukuanPopupRef" />
  <XianjinPopup ref="XianjinPopupRef" />
</template>
<script setup>
import { ref } from "vue";
import PaytypeList from "./PaytypeList.vue";
const PaytypeListRef = ref()
import ZhekouPopup from "./ZhekouPopup.vue";
import ChuzhifukuanPopup from "./ChuzhifukuanPopup.vue";
import XianjinPopup from "./XianjinPopup.vue";
const tkInputRef = ref()
const ChuzhifukuanPopupRef = ref()
const ZhekouPopupRef = ref()
const XianjinPopupRef = ref()

const showPopup = ref(false);



const selectType = ref(1);
const hejiValue = ref("160.00");
const yingshouValue = ref("160.00");
const yishouValue = ref("160.00");
const shifuValue = ref("160.00");
const weishouValue = ref("160.00");
const zhifumaValue = ref("");
const open = () => {
  zhifumaValue.value = "";
  selectType.value = 1;
  showPopup.value = true;
};
const close = () => {
  showPopup.value = false;
};

function keyD(e) {
  switch (e) {
    case "退格":
      if (selectType.value == 1) {
        shifuValue.value = shifuValue.value.slice(0, -1);
      } else {
        zhifumaValue.value = zhifumaValue.value.slice(0, -1);
      }
      break;
    case "清除":
      if (selectType.value == 1) {
        shifuValue.value = "";
      } else {
        zhifumaValue.value = "";
      }
      break;
    case ".":
      if (selectType.value == 1 && !shifuValue.value.includes(".")) {
        shifuValue.value = shifuValue.value + ".";
      } else if (!zhifumaValue.value.includes(".")) {
        zhifumaValue.value = zhifumaValue.value + ".";
      }
      break;

    default:
      if (selectType.value == 1) {
        // 判断实付金额大于未收金额
        if (Number(shifuValue.value) > Number(weishouValue.value))
          return (shifuValue.value = weishouValue.value);
        else if (
          shifuValue.value &&
          shifuValue.value.split(".")[1]?.length >= 2
        )
          return;
        shifuValue.value = shifuValue.value + e;
      } else {
        zhifumaValue.value = zhifumaValue.value + e;
      }
      break;
  }
}


function switchSelectType(type) {
  selectType.value = type;
}

function openMorePayList() {
  PaytypeListRef.value.open()
}

function onConfirm(e) {
  console.log(e);
}

defineExpose({ open, close });
</script>
<style lang="scss" scoped>
.tk-input {
  font-size: 16rpx;
  text-align: right;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}
.cursor {
  width: 1px;
  height: 20rpx;
  background-color: $uni-text-color-3;
  margin-left: 1rpx;
  animation: blink 1s step-start infinite;
}

.popup-box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 999;
  // display: flex;x@
  .popup-body {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 840rpx;
    background: $uni-text-color-inverse;
    display: flex;

    .body-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 16rpx 30rpx;
      background: $uni-text-color-inverse;
    }
    .left,
    .right {
      width: 0;
      flex: 1;
      height: 100%;
    }
    .left {
      display: flex;
      flex-direction: column;
      padding: 30rpx 40rpx;
      color: $uni-text-color-1;
      .left-scroll-box {
        background: $uni-border-color-3;
        padding: 3rpx 20rpx;
        border-radius: 4rpx;
        .item {
          margin-bottom: 6rpx;
          display: flex;
          justify-content: space-between;
          color: $uni-text-color-2;
          line-height: 24rpx;

          overflow-y: scroll;
          &:nth-last-child(1) {
            margin-bottom: 0;
          }
        }
      }
      .left-item_1 {
        border-bottom: 1rpx dashed $uni-border-color;
      }
      .left-item_2 {
        padding: 8rpx 20rpx;
        color: $uni-text-color-2;
        border: 1rpx solid $uni-border-color;
        border-radius: 8rpx;
      }
      .active {
        border-color: $uni-color-primary;
      }

      .left-bottom {
        margin-top: auto;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 20rpx;
        > div {
          flex: 1;
          width: 0;
          line-height: 40rpx;
          border: 1rpx solid $uni-color-primary;
          color: $uni-color-primary;
          text-align: center;
          border-radius: 8rpx;
          &:nth-child(2) {
            color: $uni-text-color-2;
            border-color: $uni-text-color-2;
          }
        }
      }
    }
    .right {
      padding: 20rpx;
      background: rgba(226, 229, 234, 1);

      .member_box {
        background: url("@/static/images/sales_cashier/member_bg.png") no-repeat
          center;
        height: 100rpx;
      }

      .pay_list {
        margin-top: 10rpx;
        display: flex;
        flex-wrap: wrap;
        text-align: center;
        font-size: 18rpx;
        border-radius: 8rpx;
        overflow: hidden;
        .pay_item {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 33.33%;
          height: 70rpx;
          border-bottom: 1px solid $uni-border-color-1;
          background: #fff;
          &:nth-child(3n + 2) {
            border-right: 1rpx solid $uni-border-color-1;
            border-left: 1rpx solid $uni-border-color-1;
          }
          &:nth-last-child(-n + 3) {
            border-bottom: 0;
          }
        }
      }

      .num-keyboard {
        display: flex;

        margin-top: 16rpx;

        .num {
          display: flex;
          align-items: center;
          justify-content: center;

          width: calc(33.333% - 8rpx);
          aspect-ratio: 84 / 68;
          background: #fff;
          border-radius: 8rpx;
          &:active {
            background-color: #f5f5f5;
          }
        }
        .keyboard-right {
          width: calc(25% - 6rpx);
          // padding-left: 8rpx;
          .num {
            width: 100%;
            margin-bottom: 8rpx;
          }

          .confirm {
            margin-bottom: 0;
            aspect-ratio: 84 / 144;
            background: $uni-color-primary;
            color: $uni-text-color-inverse;
          }
        }
      }
    }
  }
}




</style>
